﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Zzw
{
    public static class CompareHelper
    {
        /// <summary>
        /// Url按照字符串长度比较
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public static int CompareUrlTo(this string x,string y)
        {
            int returnCompare = x.Length.CompareTo(y.Length);
            if (returnCompare != 0)
            {
                return returnCompare;
            }
            else
            {
                return x.CompareTo(y);
            }
        }
        /// <summary>
        /// Url按照字符串长度比较
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public static int CompareTitleTo(this string x, string y, Regex regex,int index)
        {
            var reX = regex.Match(x);
            Match reY;
            if (reX.Success)
            {
                reY = regex.Match(y);
                if (reY.Success)
                {
                    string sx, sy;
                    sx = reX.Groups[index].ToString();
                    sy = reY.Groups[index].ToString();
                    if(sx.IsInt() && sy.IsInt())
                    {
                        return sx.ToInt().CompareTo(sy.ToInt());
                    }
                    else if (sx.IsIntChinese() && sy.IsIntChinese())
                    {
                        return sx.ToIntChinese().CompareTo(sy.ToIntChinese());
                    }
                }
            }
            return x.CompareTo(y);
        }
        /// <summary>
        /// 比较两个IP地址的大小
        /// </summary>
        /// <param name="ipx">要比较的第一个对象</param>
        /// <param name="ipy">要比较的第二个对象</param>
        /// <returns>比较的结果.如果相等返回0，如果x大于y返回1，如果x小于y返回-1</returns>
        public static int CompareIpTo(this string ipx, string ipy)
        {
            string[] ipxs = ipx.Split('.');
            string[] ipys = ipy.Split('.');
            for (int i = 0; i < 4; i++)
            {
                if (Convert.ToInt32(ipxs[i]) > Convert.ToInt32(ipys[i]))
                {
                    return 1;
                }
                else if (Convert.ToInt32(ipxs[i]) < Convert.ToInt32(ipys[i]))
                {
                    return -1;
                }
                else
                {
                    continue;
                }
            }
            return 0;
        }
    }
}